﻿Imports Mystery

Public Class ContentAdd
    Implements IContentCreatorControl

    Private _content_type As ContentType
    Private _content As IContent

    Public Function canDo(content_type As System.Type) As Boolean Implements IContentCreatorControl.canDo
        Return _content_type Is Nothing OrElse _content_type.used_in = content_type
    End Function

    Public Function setUp(content_type As System.Type, content As IContent) As Boolean Implements IContentCreatorControl.setUp
        _content_type = getMystery.AssemblyRegister.getMysteryClassAttribute(Of ContentType)(content_type)
        If content Is Nothing Then
            _content = getGlobalObject(Of IGlobalContentCreator).getNewContent(content_type)
        Else
            _content = content
        End If


        generateControl()

        For Each pc As IPropertyControl In _controls
            pc.content = _content
        Next

        Save.Content = "Save " & _content_type.label
        Return True
    End Function

    Public Event created(content As Mystery.IContent) Implements IContentCreatorControl.created

    Private Sub Save_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles Save.Click
        For Each pc As IPropertyControl In _controls
            pc.setInContent()
        Next
        Dim cd As IContentDispatcher = getGlobalObject(Of IContentDispatcher)()
        cd.Add(_content)
        RaiseEvent created(_content)
    End Sub


    Private _controls As New List(Of IPropertyControl)

    Private Sub generateControl()

        Dim property_controls As New HashSet(Of ContentPropertyAddAttr)( getMystery.AssemblyRegister.getMysteryPropertyAttributes(Of ContentPropertyAddAttr)((_content.GetType())))
        Dim index As Integer = 0
        Dim rd As RowDefinition
        For Each pc As ContentPropertyAddAttr In property_controls

            rd = New RowDefinition
            stack.RowDefinitions.Add(rd)
            Dim c As UserControl = pc.createControl(_content)

            Grid.SetRow(c, index)
            Grid.SetColumn(c, 1)
            stack.Children.Add(c)

            If TypeOf (c) Is IPropertyControl Then
                Dim property_control As IPropertyControl = c
                'labeling
                Dim plabel As New Label
                plabel.Content = property_control.label
                Grid.SetRow(plabel, index)
                Grid.SetColumn(plabel, 0)
                stack.Children.Add(plabel)
                _controls.Add(c)
                rd.Height = property_control.wished_height
            End If

            index += 1
        Next
        rd = New RowDefinition
        rd.Height = New GridLength(50.0)
        stack.RowDefinitions.Add(rd)
        Grid.SetRow(Save, index + 1)

    End Sub



End Class
